using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint.Publishing.WebControls;
using ARF.Web.Controls;
using System.Xml;
using Microsoft.SharePoint;

namespace ARF.Publishing.Web.Fields
{
    public class MediaField : RichLinkField
    {
        private XslRenderControl _xsl;
        private string _objectParams;
        private string _objectAttributes;
        private string _embedAttributes;

        public string EmbedAttributes
        {
            get { return _embedAttributes; }
            set { _embedAttributes = value; }
        }

        public string ObjectAttributes
        {
            get { return _objectAttributes; }
            set { _objectAttributes = value; }
        }


        public string ObjectParams
        {
            get { return _objectParams; }
            set { _objectParams = value; }
        }

        public XslRenderControl Xsl
        {
            get { if (_xsl == null) _xsl = XslRenderControl.Create(this); return _xsl; }
        }

        protected override void RenderFieldForDisplay(System.Web.UI.HtmlTextWriter output)
        {
            //base.RenderFieldForDisplay(output);
            string sHtml = "<div>" + ((ItemFieldValue != null) ? ItemFieldValue.ToString() : "") + "</div>";

            StringBuilder oOutput = new StringBuilder();
            ARF.Web.Utility.HtmlReader oReader = new ARF.Web.Utility.HtmlReader(sHtml);
            ARF.Web.Utility.HtmlWriter oWriter = new ARF.Web.Utility.HtmlWriter(oOutput);

            oReader.Read();
            while (!oReader.EOF) oWriter.WriteNode(oReader, true);

            oWriter.Flush();
            oWriter.Close();

            XmlDocument oDoc = new XmlDocument();
            oDoc.LoadXml(oOutput.ToString());
            Xsl.XmlObject = oDoc;

            if (string.IsNullOrEmpty(Xsl.XslName))
                Xsl.XslAsString = "";

            string sParams = null, sObjectAtt = null, sEmbedAtt = null;

            try
            {
                if (oDoc.DocumentElement.FirstChild.Name == "a")
                {
                    string sUrl = ((XmlElement)oDoc.DocumentElement.FirstChild).GetAttribute("href");
                    SPListItem oItem = SPContext.Current.Site.RootWeb.GetListItem(sUrl);

                    if (oItem.Fields.ContainsField("Params"))
                        sParams = (string)oItem["Params"];

                    if (oItem.Fields.ContainsField("ObjectAttributes"))
                        sObjectAtt = (string)oItem["ObjectAttributes"];

                    if (oItem.Fields.ContainsField("EmbedParams"))
                        sEmbedAtt = (string)oItem["EmbedParams"];
                }
            }
            catch {}

            if (!string.IsNullOrEmpty(ObjectParams)) sParams = ObjectParams;
            if (!string.IsNullOrEmpty(ObjectParams)) sObjectAtt = ObjectAttributes;
            if (!string.IsNullOrEmpty(ObjectParams)) sEmbedAtt = EmbedAttributes;

            oDoc.DocumentElement.SetAttribute("Params", sParams);
            oDoc.DocumentElement.SetAttribute("ObjectAttributes", sObjectAtt);
            oDoc.DocumentElement.SetAttribute("EmbedAttributes", sEmbedAtt);

            Xsl.RenderControl(output);
        }
    }
}
